/* $('#btn').click(function () {
    const ordernum = $('#orders').val()
    const ocar = $('#plate_number').val()
    if (!ordernum && !ocar) {
        alert('请输入工单号或者车牌号')
    } else {
        var url = `http://127.0.0.1:3000/maintenance?ordernum=${ordernum}&ocar=${ocar}`
        $.get(url, data => {
            console.log(data.data);
            $('#tbody').html(
                data.data.map(value => {
                    const { oid, pcode, pname, sum, aname, price, ocar, ordernum, ctime } = value
                    $('table caption').html(
                        `车牌号:<span>${ocar}</span>工单号:<span>${ordernum}</span>工单创建时间:<span>${moment(ctime).format('YYYY/MM/DD HH:mm:ss')}</span>`
                    )
                    return `
                <tr>
                    <td>${oid}</td>
                    <td><input type="checkbox" onclick="getCheck()"></td>
                    <td>${pcode}</td>
                    <td>${pname}</td>
                    <td>${aname}</td>
                    <td>${sum}</td>
                    <td>${price}</td>
                    <td>${price * sum}</td>
                </tr>
                `
                })
            )
            function getTotal() {
                let total = 0
                data.data.forEach(e => {
                    // console.log(e);
                    total += e.sum * e.price
                });
                return total
            }
            $('tfoot').html(
                `<tr>
                <td colspan="8">总金额￥:${getTotal()}</td>
            </tr>`
            )
        })
    }
}) */
$('#btn').click(function () {
    $('#btn').attr('disabled', "true")
    const ordernum = $('#orders').val()
    const ocar = $('#plate_number').val()
    if (!ordernum && !ocar) {
        $('#All').slideDown()
        var url = `http://127.0.0.1:3000/maintenance/distinct?ordernum=${ordernum}&ocar=${ocar}`
        $.get(url, data => {
            console.log(data.data);
            $('#All').html(
                data.data.map(value => {
                    const { ocar, ordernum } = value
                    console.log(ocar, ordernum);
                    return `
                    <li onclick="getNum(${ordernum},this)"><span>车牌号：<b>${ocar}</b></span><span>工单号：${ordernum}</span></li>
                    `
                })
            )
        })
    } else {
        getData(ordernum, ocar)
    }
    $('#addMess').hide()
})
$('#orders').blur(function () {
    const ordernum = $(this).val()
    console.log(ordernum);
    if (ordernum) {
        $('#btn').removeAttr("disabled")
    }
})
$('#plate_number').blur(function () {
    const ocar = $(this).val()
    console.log(ocar);
    if (ocar) {
        $('#btn').removeAttr("disabled")
    }
})
function getNum(el, ele) {
    // console.log(el,ele);
    var ocar = $(ele).children().eq(0).children().html()
    console.log(el, ocar);
    getData(el, ocar)
    $('#All').hide(1000)
}
function getData(e, p) {
    var url = `http://127.0.0.1:3000/maintenance?ordernum=${e}&ocar=${p}`
    $.get(url, data => {
        console.log(data.data);
        $('#tbody').html(
            data.data.map(value => {
                const { oid, pcode, pname, sum, aname, price, ocar, ordernum, ctime } = value
                $('table caption').html(
                    `车牌号:<span>${ocar}</span>工单号:<span>${ordernum}</span>工单创建时间:<span>${moment(ctime).format('YYYY/MM/DD HH:mm:ss')}</span>`
                )
                return `
            <tr>
                <td>${oid}</td>
                <td><input type="checkbox" onclick="getCheck()"></td>
                <td>${pcode}</td>
                <td>${pname}</td>
                <td>${aname}</td>
                <td>${sum}</td>
                <td>${price}</td>
                <td>${price * sum}</td>
            </tr>
            `
            })
        )
        function getTotal() {
            let total = 0
            data.data.forEach(e => {
                // console.log(e);
                total += e.sum * e.price
            });
            return total
        }
        $('tfoot').html(
            `<tr>
            <td colspan="8">总金额￥:${getTotal()}</td>
        </tr>`
        )
    })
}
//  声明一个变量保存全选input
var ckAll = document.getElementById("checkAll");
//  声明一个变量 通过标签名选择器保存tbody中所有的input标签
var cks = document.getElementById("tbody").getElementsByTagName("input");

//  新增按钮显示出输入框
$('#addBTN').click(function () {
    if ($('tbody tr td').html()) {
        $('#addMess').fadeIn(1500)
    } else {
        alert('请先输入工单号或者车牌号')
    }
})

//  删除按钮点击事件
$('#delBtn').click(function () {
    $('#addMess').hide()
    if ($('tbody tr td').html() == true) {
        var oid = $(':checkbox:checked').parent().prev().html()
        console.log(oid);
        if (confirm('确定要删除该项数据吗')) {
            var url = `http://127.0.0.1:3000/maintenance/delete`
            var params = { oid }
            $.post(url, params, data => {
                if (data.code == 200) {
                    alert('该项数据删除成功')
                    $('#btn').click()
                } else {
                    alert('还未选择要删除的项目')
                }
            })
        }
    } else {
        alert('请选择要删除的数据')
    }
})
//  保存按钮获取输入框的值
$('#addBtn').click(function () {
    const ocar = $('table caption').children().eq(0).html()
    const ordernum = $('table caption').children().eq(1).html()
    const ocode = $('#pcode').val()
    const sum = $('#sum').val()
    const params = { ocar, ordernum, ocode, sum }
    console.log(params);
    var url = `http://127.0.0.1:3000/maintenance/add`
    if (ocar && ordernum && ocode && sum) {
        $.post(url, params, res => {
            console.log(res);
            if (res.code == 200) {
                alert('工单新增数据成功')
                $('#btn').click()
                $('#addMess').fadeOut(1500)
            } else {
                alert('工单新增数据失败')
            }
        })
    } else {
        alert('还未输入要新增的信息')
    }
    $('#addMess').hide()
})

//  全选框的点击事件
ckAll.onclick = function () {
    console.log(2);
    for (var i = 0; i < cks.length; i++) {
        cks[i].checked = this.checked;
    }
};
//  全部勾选之后全选框被默认勾选
//  因为tbody中的input是动态渲染出来的存在于异步，所以它的点击事件不能直接通过上面声明的变量名来执行
//  在渲染的input type="checkbox"标签中 绑定一个onclick="getCheck()"
//  每次点击input框的时候 都会调用下面这个函数
function getCheck() {
    for (var i = 0; i < cks.length; i++) {
        // console.log(1);
        var flag = true;
        for (var j = 0; j < cks.length; j++) {
            if (!cks[j].checked) {
                flag = false;
                break;
            }
        }
        ckAll.checked = flag;
    }
}
$('#upBtn').click(function () {
    var oid = $(':checkbox:checked').parent().prev().html()
    console.log(oid);
    if ($(':checkbox:checked').prop('checked')) {
        $('#addMess').show()
        const ocar = $('table caption').children().eq(0).html()
        const ordernum = $('table caption').children().eq(1).html()
        const ocode = $('#pcode').val()
        const sum = $('#sum').val()
        let params = { oid, ocar, ordernum, ocode, sum }
        console.log(params);
        if (ocar && ordernum && ocode && sum && oid) {
            var url = `http://127.0.0.1:3000/maintenance/update`
            $.post(url, params, data => {
                if (data.code == 200) {
                    alert('工单修改成功')
                    $('#btn').click()
                } else {
                    alert('工单修改失败')
                }
            })
        }
    } else {
        alert('请勾选要修改的项目')
    }
})

//  入账按钮点击事件
$('#entryBtn').click(function () {
    if (($('tbody tr td').html()) && $('#checkAll').prop('checked')) {
        const ordernum = $('table caption').children().eq(1).html()
        const etime = new Date().toLocaleString()
        console.log(etime, ordernum);
        const params = { ordernum, etime }
        var url = `http://127.0.0.1:3000/maintenance/uptime`
        $.post(url, params, data => {
            console.log(data);
            if (data.code == 200) {
                var $tr = $("tbody").children()
                console.log($tr);
                for (var i = 0; i < $tr.length; i++) {
                    var utime = new Date().toLocaleString()
                    var pcode = $tr[i].children[2].innerHTML
                    var bank = $tr[i].children[5].innerHTML * 1
                    var params = { bank, utime, pcode }
                    console.log(params);
                    var url = `http://127.0.0.1:3000/partmessage/upt`
                    $.post(url, params, data => {
                        console.log(data);
                        if (data.code == 200) {
                            location.reload()
                            console.log(111111);
                        } else {
                            console.log(22222222);
                        }
                    })
                }
                alert('工单入账成功')
                window.location = '#p=maintenance'
            } else {
                alert('工单入账失败')
            }
        })
    } else {
        alert('您还有未勾选的维修项目')
    }
    $('#addMess').hide()
})

